package net.sx.minemodule.mvp.activities;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.aroutercomment.UserService;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.dialog.ECAlertDialog;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.receiver.ActivitiesManager;
import com.kedll.library.util.DataCleanManager;
import com.kedll.library.util.Log;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.SharePreUtils;
import com.network.update.IGetUpdateInfo;
import com.network.utils.UpdateUtil;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineActivityMineSettingBinding;

import java.io.File;

import io.reactivex.functions.Consumer;

/**
 * 设置
 *
 * @author 11470
 */
@Route(path = ARouterPath.PATH_MINE_SETTING)
public class MineSettingActivity extends MyBaseFragmentActivity<MineActivityMineSettingBinding, BasePresenter> {
    @Autowired(name = ARouterPath.PATH_USER_SERVICE)
    public UserService userService;
    /**
     * 缓存大小
     */
    private String cacheSize;

    @Override
    protected int getLayout() {
        return R.layout.mine_activity_mine_setting;
    }

    @Override
    protected void initEvent() {
        // 个人资料
        mBinding.entryUserInfo.setOnClickListener(v -> {
            ARouter.getInstance().build(ARouterPath.PATH_MINE_SETTING_USER_INFO).navigation();
        });

        //账户安全
//        mBinding.entryUserAccount.setOnClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_ACCOUNT_SAFETY).navigation());
        //修改密码
        //del shibo.zheng 19/06/10 start
        /*mBinding.entryLoginPasswordSafety.setOnClickListener(v -> {
            ARouter.getInstance().build(ARouterPath.PATH_MINE_UPDATE_LOGIN_PASSWORD).navigation();
        });

        //支付密码
        mBinding.entryPayPasswordSafety.setOnClickListener(v -> {
            ARouter.getInstance().build(ARouterPath.PATH_MINE_UPDATE_PAY_PASSWORD).navigation();
        });*/
        //del shibo.zheng 19/06/10 end

        //清除缓存
        mBinding.entryClearCache.setOnClickListener(v -> {
            ECAlertDialog dialog = ECAlertDialog.buildAlert(this, "确定要清除缓存数据吗?", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog1, int which) {
                    DataCleanManager.cleanInternalCache(MineSettingActivity.this.getContext());//清除内部缓存...
                    DataCleanManager.cleanExternalCache(MineSettingActivity.this.getContext());
                    MineSettingActivity.this.getCache();   //获取剩余的缓存数据并显示
                    dialog1.dismiss();
                }
            });
            dialog.show();
        });

        //add shibo.zheng 19/06/10 start
        //关于我们
        mBinding.entryAboutMine.setOnClickListener(v -> {
            ARouter.getInstance().build(ARouterPath.PATH_MINE_ABOUT_MINE).navigation();
        });
        //add shibo.zheng 19/06/10 end

        //消息推送
        //del shibo.zheng 19/06/10 start
//        mBinding.entryMsgSetting.setOnClickListener(v -> ARouter.getInstance().build(ARouterPath.PATH_MINE_MSG_PUSH).navigation());
        //del shibo.zheng 19/06/10 end

        //退出登录
        mBinding.tvLogOut.setOnClickListener(v -> {
            MineApiUtils.logout(jsonObject -> {
                android.util.Log.d(TAG, "accept: succc  " + jsonObject);
                /*userService.outLogin();
                ActivitiesManager.getInstance().finish();
                ARouter.getInstance().build(ARouterPath.PATH_USER_LOGIN).navigation();*/
            }, throwable -> android.util.Log.d(TAG, "accept: errrrrr"));
        });

        mBinding.tvLogOut1.setOnClickListener(v -> {
            userService.outLogin();
            ActivitiesManager.getInstance().finish();
            ARouter.getInstance().build(ARouterPath.PATH_USER_LOGIN).navigation();
        });

        //给宅品直销评分
        //del shibo.zheng 19/06/10 start
//        mBinding.entryMinuteSetting.setOnClickListener(v -> MyUtils.launchAppDetail(this, getPackageName(), null));
        //del shibo.zheng 19/06/10 end

        //版本号
        //del shibo.zheng 19/06/10 start
        /*mBinding.entryVersionSetting.setOnClickListener(v -> {
            UpdateUtil.softWareUpdate(getContext(), getSupportFragmentManager(), true, new IGetUpdateInfo() {
                @Override
                public boolean isUpgrade(JSON d) {
                    return false;
                }

                @Override
                public String getUpdateInfo(JSON d) {
                    return null;
                }

                @Override
                public String getUpdateUrl(JSON d) {
                    return null;
                }
            });
        });*/
        //del shibo.zheng 19/06/10 end
    }

    /**
     * 获取缓存数据
     */
    public void getCache() {
        File InstoreCache = getCacheDir();
        File ExternalCache = getExternalCacheDir();
        Log.i("cache_path", InstoreCache.getAbsolutePath().toString());
        Log.i("cache_path", ExternalCache.getAbsolutePath());
        try {
            long InstoreCacheSize = DataCleanManager.getFolderSize(InstoreCache);
            long ExternalCacheSize = DataCleanManager.getFolderSize(ExternalCache);
            cacheSize = DataCleanManager.getFormatSize(InstoreCacheSize);
            Log.i("cache_path_In", DataCleanManager.getFormatSize(DataCleanManager.getFolderSize(InstoreCache)));//内部缓存
            Log.i("cache_path_Ex", DataCleanManager.getFormatSize(DataCleanManager.getFolderSize(ExternalCache)));//外部缓存
            Log.i("cache_path_size", InstoreCacheSize
                    + ExternalCacheSize + "----" + cacheSize);
            if (cacheSize.equals("0.0Byte")) {
                mBinding.entryClearCache.setLeftTextAndRightTextRightImg("清除缓存", "0MB", false);
            } else {
                mBinding.entryClearCache.setLeftTextAndRightTextRightImg("清除缓存", cacheSize, false);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mBinding.entryUserInfo.setLeftTextAndRightImage("个人资料", true);
        //del shibo.zheng 19/06/10 start
//        mBinding.entryUserAccount.setLeftTextAndRightImage("账户安全", true);
//        mBinding.entryLoginPasswordSafety.setLeftTextAndRightImage("修改密码",true);
//        mBinding.entryPayPasswordSafety.setLeftTextAndRightImage("支付密码",true);
        mBinding.entryClearCache.setLeftTextAndRightImage("清除缓存", true);
        mBinding.entryAboutMine.setLeftTextAndRightImage("关于我们", true);
//        mBinding.entryMsgSetting.setLeftTextAndRightImage("消息推送", true);
//        mBinding.entryMinuteSetting.setLeftTextAndRightImage("给宅品直营评分", true);
//        mBinding.entryVersionSetting.setLeftTextAndRightTextRightImg("版本号", "V" + MyUtils.getVersionCode(this), false);
        //del shibo.zheng 19/06/10 end

        getCache();

    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("设置");
        ARouter.getInstance().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }
}
